package cn.lanqiao.ui;

import java.awt.EventQueue;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.dao.UserDao;
import cn.lanqiao.dao.impl.UserDaoImpl;

import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class TeacherSources extends JInternalFrame {

	private static final long serialVersionUID = 2251013759564171985L;
	private JTable table;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TeacherSources frame = new TeacherSources();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public TeacherSources() {
		setClosable(true);
		setTitle("学生成绩");
		setBounds(100, 100, 671, 386);
		getContentPane().setLayout(null);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
		scrollPane.setBounds(7, 0, 641, 221);
		getContentPane().add(scrollPane);
		table = new JTable();
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		table.setFillsViewportHeight(true);
		table.setRowHeight(30);
		
		//设置表格数据居中
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
		cr.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, cr);
		
		cn.lanqiao.service.ScoreService service = new cn.lanqiao.service.impl.ScoreServiceImpl();
		table.setModel(new DefaultTableModel(service.lists(), new String[] { "学号", "姓名", "科目", "成绩" }));
		scrollPane.setViewportView(table);

		JButton ExportScore = new JButton("成绩导出");
		ExportScore.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		ExportScore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 成绩导出
				// 导出
				String[] headers = { "学号", "姓名", "科目", "成绩" };
				cn.lanqiao.service.UserService score = new cn.lanqiao.service.impl.UserServiceImpl();
				DefaultTableModel model = new DefaultTableModel(score.getAllScores(), headers);
				final JTable table = new JTable(model);
				UserDao export = new UserDaoImpl();
				try {
					export.exportTable(table, new File("成绩信息.txt"));
					File file = new File("..\\Edu_admin_system\\成绩信息.txt");
					if (file.exists()) {
						JOptionPane.showMessageDialog(null, "导出成功不成功自己没点那啥数？");
						System.out.println("文件存在");
					} else {
						System.out.println("不存在");
					}
				} catch (IOException e1) {
					e1.printStackTrace();
					System.out.println(e1.getMessage());
				}
			}
		});
		ExportScore.setBounds(511, 265, 130, 37);
		getContentPane().add(ExportScore);
	}
}
